<?php namespace App\Http\Controllers\user\business;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use App\Models\BusinessCircle;
use App\Models\BusinessComment;
use App\Models\Users;

class IndexController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$recUser = Users::with(['profile'])->where("validate_state", 2)->where("is_rec_business_index", 1)->orderBy("business_order", "desc")->take(10)->get()->toArray();
		// dd($recUser);
		return view('user.business.index', ["recUser" => $recUser])->withIsBusiness(true);
	}

	public function addMessage()
	{
		$uid = $this->getUid();
		if($uid)
		{
			$message = Input::get("message");
			$model = new BusinessCircle;
			$model->content = $message;
			$model->uid = $uid;
			$model->save();
			$data = BusinessCircle::with(['profile'])->where("id", $model->id)->get()->toArray();
			return $this->json(["data" => $data]);
		}
		else
		{
			return;
		}
	}


	public function getMessageList()
	{
		$page = Input::get("page");
		$row = Input::get("row");
		$data = BusinessCircle::with(["profile"])->orderBy("id", "desc")->skip(($page - 1)*$row)->take($row)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function addComment()
	{
		$uid = $this->getUid();
		if($uid)
		{
			$cir_id = Input::get("cir_id");
			$content = Input::get("content");
			$model = new BusinessComment;
			$model->circle_id = $cir_id;
			$model->uid = $uid;
			$model->content = $content;
			$model->save();
			$cir_model  = BusinessCircle::find($cir_id);
			$cir_model->comment_num = $cir_model->comment_num + 1;
			$cir_model->update();
			$data = BusinessComment::with(["profile"])->where("id", $model->id)->get()->toArray();
			return $this->json(["data" => $data]);
		}
		else
		{
			return;
		}
	}

	public function getCommentList()
	{
		$page = Input::get("page");
		$row = Input::get("row");
		$cir_id = Input::get("cir_id");
		$model = BusinessComment::with(["profile"])->where("circle_id", $cir_id);
		$count = $model->count();
		$data = $model->orderBy("id", "desc")->skip(($page - 1)*$row)->take($row)->get()->toArray();
		return $this->json(["data" => $data, "count" => $count]);
	}

	// 后台用户列表

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
